import Desktop from './component/Desktop.js';
import Mouse from './component/Mouse.js';
import TaskBar from './component/TaskBar.js';
import Win from './component/Win.js';
import Http from './component/Http.js';
import User from './component/User.js';
import Util from './Util.js';

// import Msg from './component/Message.js';
window.onload = function() {
    new User(userName, '').login((user) => {
        if (!user.loginStatus) {
            window.location.href = './html/login.html';
        } else {
            Http.ajax({
                url: `/app/${user.name}`,
                success: function (data) {
                    // 从服务器获取到应用信息
                    const app = JSON.parse(data.response);
    
                    // 传入的是桌面控件id
                    // const desktop = new Desktop('desktopCtrl', appProxy);
                    const desktop = new Desktop('desktopCtrl', app);
                    // 生成桌面网格
                    desktop.build('desktopIconContainer');
                    // 绑定桌面图标事件：hover、drag等
                    desktop.bindIcon('desktop-icon');
                    // 任务栏设置，包含通知中心，日历组件
                    new TaskBar(desktop);
                    // 加载窗口缓存
                    new Win(desktop);
                    // 导入储存的窗口信息
                    Win.buildFromCache(desktop);
                    // 鼠标右键事件，未完成，后续还有很多数据加入和判断
                    Mouse.rightKey(desktop);
                    // 新建Socket.io网络通信，绑定通讯事件
                    const socket = io.connect('ws://localhost:3000');
                    new Http(socket);
    
                    // 向其他应用暴露对象
                    // TODO 需要用户许可
                    window.desktop = desktop;
                    window.socket = socket;
                    window.Win = Win;
                },
                error: function () {
                    Util.globalMessage('获取基础数据失败！', 'error');
                }
            });
        }
    });
}

